package org.gis.mapserver.layer;

import org.gis.mapserver.base.ConfigBean;
import org.gis.mapserver.base.Constants;
import org.gis.mapserver.rendercontext.RenderingContext;
import org.gis.mapserver.source.Source;
import org.gis.mapserver.source.SourceFactory;

/**
 *
 * @author gus
 */
public abstract class AbstractLayer implements Layer
{
    protected boolean enabled;
    protected String name;
    protected long id;
    protected ConfigBean configBean;
    protected SourceFactory sourceFactory;

    public String getName()
    {
        return name;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    public long getLayerID()
    {
        return id;
    }

    public void setLayerID( long id )
    {
        this.id = id;
    }

    public boolean isEnabled()
    {
        return enabled;
    }

    public void setEnabled( boolean enabled )
    {
        this.enabled = enabled;
    }

    public ConfigBean getLayerConfigBean()
    {
        return configBean;
    }

    public void setLayerConfigBean( ConfigBean layerConfigBean )
    {
        this.configBean = layerConfigBean;
    }

    public void setSourceFactory( SourceFactory sourceFactory )
    {
        this.sourceFactory = sourceFactory;
    }

    public void afterInitialize() throws Exception
    {
        if ( configBean == null )
        {
            throw new Exception("layerConfigBean is null");
        }

        if ( sourceFactory == null )
        {
            throw new Exception("sourceFactory is null");
        }
    }

    public void render( RenderingContext context ) throws Exception
    {
        if ( context == null )
        {
            throw new IllegalArgumentException("RenderingContext");
        }

        if ( !isEnabled() )
        {
            return;
        }
        String sourceName = configBean.getValue(Constants.SOURCE_NAME);
        if ( sourceName == null || sourceName.isEmpty() )
        {
            throw new Exception("sourceName is empty");
        }
        doRender(context, sourceFactory.makeSource(sourceName));
    }

    protected abstract void doRender( RenderingContext context, Source source ) throws Exception;
}
